/**
 * @description 房源信息服务层
 * @date 2020-08-13 15:48
 * @auth LI
 */

const { HouseInfo, Account, AcceptanceInfo } = require('../db/model/index')
const Sequelize = require('sequelize')
const Op = require('sequelize').Op
const { formatMoreTime, timeFormat } = require('../utils/dateTime')

/**
 * 获取全部房源信息
 * @param {string} houseName 业主名称
 * @param {number} houseOwnerId 业主 id
 * @param {number} supervisionId 监理 id
 * @param {number} foremanId 工长 id
 * @param {number} bricklayerId 瓦工 id
 * @param {number} carpenterId 木工 id
 * @param {number} plumberId 水电工 id
 * @param {number} painterId 油漆工 id
 * @param {number} page 第几页
 * @param {number} limit 多少条
 */
async function getHouseInfoList(
{
  houseOwnerId,
  supervisionId,
  foremanId,
  bricklayerId,
  carpenterId,
  plumberId,
  painterId,
  houseName,
  page,
  limit
}) {
  let whereOpt = {}
  if (houseName) {
    whereOpt = {
      houseName: {
        [Op.like]: '%' + houseName + '%'
      }
    }
  }
  if (houseOwnerId) {
    whereOpt.houseOwnerId = houseOwnerId
  }
  if (supervisionId) {
    whereOpt.supervisionId = supervisionId
  }
  if (foremanId) {
    whereOpt.foremanId = foremanId
  }
  if (bricklayerId) {
    whereOpt.bricklayerId = bricklayerId
  }
  if (carpenterId) {
    whereOpt.carpenterId = carpenterId
  }
  if (plumberId) {
    whereOpt.plumberId = plumberId
  }
  if (painterId) {
    whereOpt.painterId = painterId
  }
  const result = await HouseInfo.findAndCountAll({
    limit: limit, // 每页多少条
    offset: limit * (page - 1), // 跳过多少条
    where: whereOpt,
    include: [
      { model: Account, as: 'houseOwner', attributes: ['name'] },
      { model: Account, as: 'supervision', attributes: ['name'] },
      { model: Account, as: 'foreman', attributes: ['name'] },
      { model: Account, as: 'bricklayer', attributes: ['name'] },
      { model: Account, as: 'carpenter', attributes: ['name'] },
      { model: Account, as: 'plumber', attributes: ['name'] },
      { model: Account, as: 'painter', attributes: ['name'] }
    ]
  })
  result.rows.map(item => {
    // 格式化时间 多个
    item.dataValues = formatMoreTime(item.dataValues)
    item.dataValues.startTime = timeFormat(item.dataValues.startTime)
    item.dataValues.endTime = timeFormat(item.dataValues.endTime)
    item.dataValues.houseOwnerName = item.dataValues.houseOwner.name
    item.dataValues.supervisionName = item.dataValues.supervision.name
    item.dataValues.foremanName = item.dataValues.foreman.name
    item.dataValues.bricklayerName = item.dataValues.bricklayer.name
    item.dataValues.carpenterName = item.dataValues.carpenter.name
    item.dataValues.plumberName = item.dataValues.plumber.name
    item.dataValues.painterName = item.dataValues.painter.name
    delete item.dataValues.houseOwner
    delete item.dataValues.supervision
    delete item.dataValues.foreman
    delete item.dataValues.bricklayer
    delete item.dataValues.carpenter
    delete item.dataValues.plumber
    delete item.dataValues.painter
  })
  
  return result
}

/**
 * 创建房源信息
 * @param {string} houseName 业主名称
 * @param {string} houseAddress 房子地址
 * @param {string} roomType 房型
 * @param {string} startTime 开工时间
 * @param {string} drawing 图纸
 * @param {number} houseOwnerId 对应账号表业主 id
 * @param {number} supervisionId 对应账号表监理 id
 * @param {number} foremanId 对应账号表工长 id
 * @param {number} bricklayerId 对应账号表瓦工 id
 * @param {number} carpenterId 对应账号表木工 id
 * @param {number} plumberId 对应账号表水电工 id
 * @param {number} painterId 对应账号表油漆工 id
 * @param {string} remark 备注/要求
 */
async function createHouseInfo(
{
  houseName,
  houseAddress,
  roomType,
  startTime,
  drawing,
  houseOwnerId,
  supervisionId,
  foremanId,
  bricklayerId,
  carpenterId,
  plumberId,
  painterId,
  remark
}) {
  const result = await HouseInfo.create({
    houseName,
    houseAddress,
    roomType,
    startTime,
    drawing,
    houseOwnerId,
    supervisionId,
    foremanId,
    bricklayerId,
    carpenterId,
    plumberId,
    painterId,
    remark
  })
  
  return result
}

/**
 * 通过 id 更新/账号信息
 * @param {number} id id
 * @param {string} houseName 业主名称
 * @param {string} houseAddress 房子地址
 * @param {string} roomType 房型
 * @param {string} startTime 开工时间
 * @param {string} drawing 图纸
 * @param {number} status 施工状态
 * @param {number} houseOwnerId 对应账号表业主 id
 * @param {number} supervisionId 对应账号表监理 id
 * @param {number} foremanId 对应账号表工长 id
 * @param {number} bricklayerId 对应账号表瓦工 id
 * @param {number} carpenterId 对应账号表木工 id
 * @param {number} plumberId 对应账号表水电工 id
 * @param {number} painterId 对应账号表油漆工 id
 * @param {string} remark 备注/要求
 */
async function updateHouseInfoById(
{
  id,
  houseName,
  houseAddress,
  roomType,
  startTime,
  drawing,
  status,
  houseOwnerId,
  supervisionId,
  foremanId,
  bricklayerId,
  carpenterId,
  plumberId,
  painterId,
  remark
}) {
  // 拼接查询条件
  const whereData = {}
  if (houseName) {
    whereData.houseName = houseName
  }
  if (houseAddress) {
    whereData.houseAddress = houseAddress
  }
  if (roomType) {
    whereData.roomType = roomType
  }
  if (startTime) {
    whereData.startTime = startTime
  }
  if (drawing) {
    whereData.drawing = drawing
  }
  if (status) {
    whereData.status = status
    if (status === 3) {
      whereData.endTime = new Date()
    }
  }
  if (houseOwnerId) {
    whereData.houseOwnerId = houseOwnerId
  }
  if (supervisionId) {
    whereData.supervisionId = supervisionId
  }
  if (foremanId) {
    whereData.foremanId = foremanId
  }
  if (bricklayerId) {
    whereData.bricklayerId = bricklayerId
  }
  if (carpenterId) {
    whereData.carpenterId = carpenterId
  }
  if (plumberId) {
    whereData.plumberId = plumberId
  }
  if (painterId) {
    whereData.painterId = painterId
  }
  if (remark) {
    whereData.remark = remark
  }
  const result = await HouseInfo.update(whereData, {
    where: {
      id
    }
  })
  
  return result[0] > 0
}

/**
 * 通过 id 删除房源信息
 * @param {number} id id
 */
async function deleteHouseInfoById(id) {
  const result = await HouseInfo.findAll({
    where: { id },
    include: [
      { model: AcceptanceInfo }
    ],
  })

  for (const resul of result) {
    for (const child of resul.acceptanceInfos) {
      await child.destroy()
    }
    await resul.destroy()
  }
  
  return result > 0
}

module.exports = {
  createHouseInfo,
  updateHouseInfoById,
  getHouseInfoList,
  deleteHouseInfoById
}
